<?php

namespace app\cashdesk\controller;

use app\common\controller\Frontend;
use fast\Random;
use think\Db;
use think\Exception;
use think\Session;

class Goods extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';
    //商品模型
    protected $model = null;
    //商品规格模型
    protected $goods_sku_model = null;

    public function _initialize()
    {
        $this->shop_id = Session::get('shop');
        $this->model = new \app\admin\model\wanlshop\Goods;
        $this->goods_sku_model = new \app\admin\model\inventory\Sku;
    }

    public function index()
    {
        return $this->view->fetch();
    }

    //获取商品sku属性列表
    public function getgoodsskus()
    {
        $id = $this->request->param('id',0);
        $type = $this->request->param('type','goods');
        $list = [];

        if (in_array($type,['goods','services'])) {
            $condition = [];
            $condition['status'] = 'normal';
            $goods_info = $this->model->where($condition)->field('id,title,image,code,vip_price')->find($id);
            if (empty($goods_info)) {
                $this->error('商品规格无法查看！');
            }

            $condition = [];
            $condition['status'] = 'normal';
            $condition['goods_id'] = $id;
            $condition['state'] = 0;
            $condition['stock'] = ['>',0];
            $goods_sku_list = collection($this->goods_sku_model->where($condition)->field('id,difference,price,stock')->select())->toArray();
            $data['goodsInfo'] = $goods_info;
            $data['goodsSku'] = $goods_sku_list;
            $this->success('商品规格获取成功！', '', $data);
        }

        $this->error('数据加载失败！');
    }
}
